#!/bin/bash

# DBC Editor VSCode Plugin 测试脚本
echo "🧪 Testing DBC Editor VSCode Plugin..."

# 检查插件文件是否存在
if [ ! -f "dbc-editor-vscode-1.3.2.vsix" ]; then
    echo "❌ Plugin file dbc-editor-vscode-1.3.2.vsix not found!"
    echo "Please run: ./build.sh --package"
    exit 1
fi

echo "✅ Plugin file found: $(ls -lh *.vsix | awk '{print $5}') $(ls *.vsix)"

# 检查VSCode是否安装
if ! command -v code &> /dev/null; then
    echo "⚠️ VSCode command 'code' not found in PATH"
    echo "Please make sure VSCode is installed and 'code' command is available"
    echo "You can still install the plugin manually in VSCode:"
    echo "   Ctrl+Shift+P → 'Extensions: Install from VSIX...' → Select the .vsix file"
    exit 0
fi

echo "✅ VSCode found: $(code --version | head -1)"

# 检查示例文件
if [ ! -f "example.dbc" ]; then
    echo "⚠️ Example DBC file not found"
else
    echo "✅ Example DBC file available"
fi

echo ""
echo "🚀 Installation Instructions:"
echo "1. Install the plugin:"
echo "   code --install-extension dbc-editor-vscode-1.3.2.vsix"
echo ""
echo "2. Restart VSCode"
echo ""
echo "3. Test the plugin:"
echo "   code example.dbc"
echo ""
echo "📋 Expected Results:"
echo "✓ DBC file opens with visual table editor"
echo "✓ CAN messages displayed in table format"
echo "✓ Signal editor shows when message is selected"
echo "✓ Signal preview displays bit layout"
echo "✓ Dark theme integration matches VSCode"
echo "✓ Auto-save works when editing"
echo ""

# 可选：自动安装插件
read -p "🤔 Do you want to automatically install the plugin now? [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "📦 Installing plugin..."
    code --install-extension dbc-editor-vscode-1.3.2.vsix
    
    if [ $? -eq 0 ]; then
        echo "✅ Plugin installed successfully!"
        echo "🔄 Please restart VSCode to activate the plugin"
        
        # 可选：自动打开测试文件
        read -p "🤔 Do you want to open the example DBC file now? [y/N] " -n 1 -r
        echo
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            echo "📂 Opening example.dbc..."
            code example.dbc
        fi
    else
        echo "❌ Plugin installation failed!"
        echo "Try manual installation in VSCode"
    fi
else
    echo "👍 Manual installation instructions provided above"
fi

echo ""
echo "🎉 Test script completed!" 